package flc.ast.dialog;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.UriUtils;
import com.stark.file.transfer.core.FileInfo;
import com.stark.file.transfer.core.TfContactInfo;
import com.stark.file.transfer.core.Transferable;
import com.stark.file.transfer.core.TransferableReceiveManager;
import com.stark.file.transfer.core.TransferableType;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.bean.SelectFileBean;
import flc.ast.bean.TransferRecordBean;
import flc.ast.databinding.DialogProgressStyleBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.bean.ContactInfo;
import stark.common.basic.constant.FileType;

/**
 * @Author: XiaoDan
 * @Date: 2023/10/23
 * @Description:
 */
public class ReceiverProgressDialog extends BaseSmartDialog<DialogProgressStyleBinding> implements TransferableReceiveManager.IReceiveListener {
    public boolean hasSuccess;
    public String serverIp;
    private int mCurrentType;
    public List<SelectFileBean> imgList, videoList, audioList, appList, contactList;


    public ReceiverProgressDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_progress_style;
    }


    @Override
    protected void initView(View view) {
        imgList = new ArrayList<>();
        videoList = new ArrayList<>();
        audioList = new ArrayList<>();
        appList = new ArrayList<>();
        contactList = new ArrayList<>();
        mDataBinding.tvTitle.setText(getContext().getString(R.string.file_receive_tips));
        TransferableReceiveManager manager = TransferableReceiveManager.getInstance();
        manager.setListener(this);
        manager.connectSender(serverIp);
    }

    @Override
    public void onGetTotalReceiveCount(int totalCount) {
        mDataBinding.sbProgress.setMax(totalCount);
    }

    @Override
    public void onCompleteCount(int totalCount, int haveReceiveCount) {
        mDataBinding.sbProgress.setProgress(haveReceiveCount);
        if (totalCount == haveReceiveCount) {
            List<TransferRecordBean> listSave = new ArrayList<>();
            String time = TimeUtils.getNowString(new SimpleDateFormat("yyyy年MM月dd日 HH:mm"));
            if (imgList.size() != 0) {
                listSave.add(new TransferRecordBean(time, 1, imgList));
            }
            if (videoList.size() != 0) {
                listSave.add(new TransferRecordBean(time, 2, videoList));
            }
            if (audioList.size() != 0) {
                listSave.add(new TransferRecordBean(time, 3, audioList));
            }
            if (appList.size() != 0) {
                listSave.add(new TransferRecordBean(time, 4, appList));
            }
            if (contactList.size() != 0) {
                listSave.add(new TransferRecordBean(time, 5, contactList));
            }
            List<TransferRecordBean> listSp = AppSpUtils.getReceiveRecordList();
            if (listSp != null && listSp.size() > 0) {
                listSp.addAll(0, listSave);
                AppSpUtils.saveReceiveRecordList(listSp);
            } else {
                AppSpUtils.saveReceiveRecordList(listSave);
            }
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (listener != null) {
                        mDataBinding.sbProgress.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                dismiss();
                                listener.clickConfirm();
                            }
                        }, 1000);
                    }
                }
            }, 1000);
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm();
    }


    @Override
    public void onReceivedTransferable(@NonNull Transferable transferable) {
        SelectFileBean bean = new SelectFileBean();
        TransferableType type = transferable.getTransferType();
        if (type == TransferableType.FILE) {
            FileInfo fileInfo = (FileInfo) transferable;
            if (fileInfo.getFileType() == FileType.IMAGE) {
                bean.setType(1);
                bean.setPath(UriUtils.uri2File(Uri.parse(fileInfo.getUriStr())).getAbsolutePath());
                bean.setName(fileInfo.getFileName());
                bean.setSize(fileInfo.getSize());
                imgList.add(bean);
            } else if (fileInfo.getFileType() == FileType.VIDEO) {
                bean.setType(2);
                bean.setPath(UriUtils.uri2File(Uri.parse(fileInfo.getUriStr())).getAbsolutePath());
                bean.setName(fileInfo.getFileName());
                bean.setSize(fileInfo.getSize());
                videoList.add(bean);
            } else if (fileInfo.getFileType() == FileType.AUDIO) {
                bean.setType(3);
                bean.setPath(UriUtils.uri2File(Uri.parse(fileInfo.getUriStr())).getAbsolutePath());
                bean.setName(fileInfo.getFileName());
                bean.setSize(fileInfo.getSize());
                audioList.add(bean);
            } else if (fileInfo.getFileType() == FileType.APK) {
                bean.setType(4);
                bean.setPath(UriUtils.uri2File(Uri.parse(fileInfo.getUriStr())).getAbsolutePath());
                bean.setName(fileInfo.getFileName());
                bean.setSize(fileInfo.getSize());
                appList.add(bean);
            }
        } else if (type == TransferableType.CONTACT) {
            TfContactInfo tfContactInfo = (TfContactInfo) transferable;
            Log.e("test", "onReceivedTransferable: " + tfContactInfo.getContactInfoList().size());
            for (ContactInfo contactInfo : tfContactInfo.getContactInfoList()) {
                SelectFileBean contactBean = new SelectFileBean();
                contactBean.setType(5);
                contactBean.setName(contactInfo.getName());
                contactBean.setPath(contactInfo.getPhone());
                contactList.add(contactBean);
            }
        }
    }
}
